//
//  MineOrderView.swift
//  Box
//
//  Created by mn on 2025/3/30.
//

import UIKit
import SnapKit

class MineOrderView: UIView {

    private let titleLabel: UILabel = {
        let label = UILabel()
        label.text = "我的订单"
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor(hex: "#E1E1E1")
        return label
    }()

    lazy var viewAllButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("查看全部 ", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor(hex: "#AAB3BF"), for: .normal)
        button.setImage(UIImage(named: "arrow_right"), for: .normal)
        button.semanticContentAttribute = .forceRightToLeft
        button.addAction {
            [weak self] in
            self?.pushToOrder()
        }
        return button
    }()

    private let orderStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.spacing = (UIScreen.main.bounds.width - 24 * 2 - 47 * 4) / 4
        return stackView
    }()

    private let orderTitles = ["待发货", "待收货", "已完成", "已取消"]
    private let orderImages = ["order_shipped", "order_received", "order_completed", "order_cancle"]

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }

    private func setupUI() {
        addSubview(titleLabel)
        addSubview(viewAllButton)
        addSubview(orderStackView)

        titleLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(16)
        }

        viewAllButton.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.right.equalToSuperview().offset(-16)
        }

        for (index, title) in orderTitles.enumerated() {
            let button = OrderButton(title: title, imageName: orderImages[index])
            orderStackView.addArrangedSubview(button)
            button.addAction {
                [weak self] in
                self?.pushToOrder(index+1)
            }
        }

        orderStackView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(21)
            make.centerX.equalToSuperview()
            make.height.equalTo(20+47+11.5)
        }
    }
    func pushToOrder(_ index: Int = 0) {
        let vc = MineOrderViewController()
        vc.selectIndex = index
        findViewController()?.navigationController?.push(vc)
    }
}
